package api.model;

import m.common.model.FieldMeta;
import m.common.model.LinkTableMeta;
import m.common.model.Model;
import m.common.model.TableMeta;
import m.common.model.config.ModelConfig;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.system.cache.CacheUtil;
import m.system.cache.FlushCache2;
import m.system.cache.FlushCacheList;
import m.system.util.ClassUtil;
import m.system.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

@TableMeta(name="ot_api_column",description="数据列")
public class ApiColumn extends Model implements FlushCache2,FlushCacheList {
    @LinkTableMeta(name="table_oid",table= ApiTable.class,description="数据表")
    private ApiTable table;

    @FieldMeta(name="name",type= FieldType.STRING,length=50,description="字段名称")
    private String name;
    @FieldMeta(name="field",type= FieldType.STRING,length=50,description="数据库字段")
    private String field;
    @FieldMeta(name="description",type=FieldType.STRING,length=200,description="描述")
    private String description;

    @FieldMeta(name="type",type= FieldType.STRING,length=20,description="类型",dictOption = "STRING:STRING,DOUBLE:DOUBLE,INT:INT,DATE:DATE,MODEL:MODEL,TABLE:TABLE")
    private String type;
    @FieldMeta(name="length",type= FieldType.INT,description="长度|STRING长度")
    private Integer length;
    @FieldMeta(name="notnull",type= FieldType.STRING, description="非空",dictOption = "Y:是,N:否")
    private String notnull;

    @LinkTableMeta(name="model_table_oid",table= ApiTable.class,description="模型表|TABLE")
    private ApiTable modelTable;
    @FieldMeta(name="model_class",type=FieldType.STRING,length=100,description="模型表|MODEL")
    private String modelClass;
    @FieldMeta(name="model_name",type=FieldType.STRING,length=50,description="来源表名|MODEL")
    private String modelName;
    @FieldMeta(name="model_desc",type=FieldType.STRING,length=100,description="来源表描述|MODEL")
    private String modelDesc;

    @FieldMeta(name="dict_type",type=FieldType.STRING,length=100,description="字典类型")
    private String dictType;
    @FieldMeta(name="dict_option",type=FieldType.STRING,length=100,description="字典选项")
    private String dictOption;

    @FieldMeta(name="isinit",type=FieldType.STRING, description="是否初始化|Y不能改",dictOption = "Y:是,N:否")
    private String isinit;

    public String getDictType() {
        return dictType;
    }

    public void setDictType(String dictType) {
        this.dictType = dictType;
    }

    public String getDictOption() {
        return dictOption;
    }

    public void setDictOption(String dictOption) {
        this.dictOption = dictOption;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public ApiTable getModelTable() {
        return modelTable;
    }

    public void setModelTable(ApiTable modelTable) {
        this.modelTable = modelTable;
    }

    public String getModelClass() {
        return modelClass;
    }

    public void setModelClass(String modelClass) {
        this.modelClass = modelClass;
    }

    public String getModelName() {
        return modelName;
    }

    public void setModelName(String modelName) {
        this.modelName = modelName;
    }

    public String getModelDesc() {
        return modelDesc;
    }

    public void setModelDesc(String modelDesc) {
        this.modelDesc = modelDesc;
    }

    public String getIsinit() {
        return isinit;
    }

    public void setIsinit(String isinit) {
        this.isinit = isinit;
    }
    public ApiTable getTable() {
        return table;
    }

    public void setTable(ApiTable table) {
        this.table = table;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public String getNotnull() {
        return notnull;
    }

    public void setNotnull(String notnull) {
        this.notnull = notnull;
    }

    /**
     * oid表名  , 字段名
     */
    @Override
    public ApiColumn getCacheModel(String key, String name) throws Exception {
        ApiColumn col=null;
        List<ApiColumn> ls=ModelQueryList.getModelList(ApiColumn.class,new String[]{"*","modelTable.*"},null,
            QueryCondition.and(
                QueryCondition.eq("table.oid",key),
                QueryCondition.eq("name",name)
            ));
        if(!ls.isEmpty()){//自建表
            col=ls.get(0);
        }else{
            List<ApiCollectColumn> list=ModelQueryList.getModelList(ApiCollectColumn.class,new String[]{"*"},null,
                QueryCondition.and(
                    QueryCondition.eq("collect.oid",key),
                    QueryCondition.eq("oid",name)
                ));
            if(!list.isEmpty()) {//汇总列
                col = new ApiColumn();
                col.setName(list.get(0).getAlias());
                col.setField(list.get(0).getField());
                col.setDescription(list.get(0).getTitle()+StringUtil.noSpace(list.get(0).getGroupTitle()));
                col.setLength(0);
                col.setType("STRING");
                col.setNotnull("N");
                col.setIsinit("Y");
                col.setDictType(list.get(0).getDict());
            }else {//系统表列
                Class<Model> clazz = (Class<Model>) ClassUtil.getClass(Model.class, key);
                FieldMeta fm = ModelConfig.getFieldMetaMap(clazz).get(name);
                if("oid".equals(name)) {
                    col = new ApiColumn();
                    col.setName(name);
                    col.setField(name);
                    col.setDescription("主键");
                    col.setLength(50);
                    col.setType("STRING");
                    col.setNotnull("Y");
                    col.setIsinit("Y");
                }else if (null != fm) {//系统表属性
                    col = new ApiColumn();
                    col.setName(name);
                    col.setField(fm.name());
                    col.setDescription(fm.description());
                    col.setLength(fm.length());
                    col.setType(fm.type().toString());
                    col.setNotnull(fm.notnull() ? "Y" : "N");
                    col.setIsinit("Y");
                    col.setDictType(fm.dictType());
                    col.setDictOption(fm.dictOption());
                } else {//系统表对象
                    LinkTableMeta lm = ModelConfig.getLinkTableMetaMap(clazz).get(name);
                    if (null != lm) {
                        col = new ApiColumn();
                        col.setName(name);
                        col.setField(lm.name());
                        col.setDescription(lm.description());
                        col.setLength(50);
                        col.setType("STRING");
                        col.setNotnull(lm.notnull() ? "Y" : "N");
                        col.setModelTable(CacheUtil.get(ApiTable.class, lm.table().getName()));
                        col.setIsinit("Y");
                    }
                }
            }
        }
        return col;
    }

    /**
     * oid表名
     */
    @Override
    public List<ApiColumn> getCacheList(String key) throws Exception {
        ApiTable at = CacheUtil.get(ApiTable.class, key);
        if (at.getSource().equals("N")||at.getSource().equals("A")||at.getSource().equals("B")){//自建表 or 视图 or 库表
            List<ApiColumn> list = ModelQueryList.getModelList(ApiColumn.class, new String[]{"*", "modelTable.*"}, null,
                QueryCondition.eq("table.oid", key)
                , QueryOrder.asc("oid"));
            for(ApiColumn column : list){
                if(!StringUtil.isSpace(column.getModelClass())){
                    column.setModelTable(CacheUtil.get(ApiTable.class,column.getModelClass()));
                }
            }
            return list;
        }else if(at.getSource().equals("C")){//汇总表
            List<ApiCollectColumn> list =ModelQueryList.getModelList(ApiCollectColumn.class,
                new String[]{"oid","collect.oid"}, null,
                QueryCondition.and(
                    QueryCondition.eq("collect.oid", key)
                ));
            List<ApiColumn> ls= new ArrayList<>();
            for(ApiCollectColumn col : list){
                ls.add(CacheUtil.get(ApiColumn.class,col.getCollect().getOid(),col.getOid()));
            }
            return ls;
        }else{//Y 系统表
            List<ApiColumn> list = new ArrayList<>();
            Class<Model> clazz= (Class<Model>) ClassUtil.getClass(Model.class,key);
            list.add(CacheUtil.get(ApiColumn.class,key,"oid"));
            for(String k : ModelConfig.getFieldMetaMap(clazz).keySet()){
                list.add(CacheUtil.get(ApiColumn.class,key,k));
            }
            for(String k : ModelConfig.getLinkTableMetaMap(clazz).keySet()){
                list.add(CacheUtil.get(ApiColumn.class,key,k));
            }
            return list;
        }
    }
}
